package com.lfx.concurrent;

import java.util.Random;
import java.util.concurrent.Semaphore;

/**
 * @author 李逢喜
 * create 2021/8/2
 */
public class MyThread extends Thread{

    private Semaphore semaphore;

    private Random random=new Random();

    public  MyThread(Semaphore semaphore,String name){
        super(name);
        this.semaphore=semaphore;
    }


    @Override
    public void run() {
        try {
            semaphore.acquire();
            System.out.println(super.getName()+"抢到座位，开始写作业");
            Thread.sleep(random.nextInt(1000));

            System.out.println(Thread.currentThread().getName() + " - 作业完 成，腾出座位");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        semaphore.release();
    }
}
